<template>
  <div class="login-container">
    <vue-particles
        color='#fff'
        >
    </vue-particles>
    <div class="login-box">
        <h1>后台登录</h1>
        <el-form :model="form" :rules="rules" ref='form'>
            <el-form-item prop="username">
                <el-input
                    autofocus
                    placeholder="用户名"
                    prefix-icon="el-icon-user"
                    v-model="form.username">
                </el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input
                    placeholder="密码"
                    type="password"
                    prefix-icon="el-icon-lock"
                    v-model="form.password"
                    show-password>
                </el-input>
            </el-form-item>
            <el-button type="primary" @click="submit('form')">登录</el-button>
            
        </el-form>
    </div>
    
  </div>
</template>

<script>
export default {
    name: 'Login',
    data() {
        return {
            form: {
                username: '',
                password: ''
            },
            loading: false,
            rules: {
                username: [
                    { required: true, message:'请填写用户名', trigger: 'blur' }
                ],
                password: [
                    { required: true, message:'请填写密码',trigger: 'blur' },
                    { min: 6, message: '密码不少于6位', trigger: 'blur' }
                ]
            },
            redirect: undefined
        }
    },
    watch: {
        $route: {
            handler: function(route) {
                // 监听路由，把路由中的redirect跳转地址保存起来。
                this.redirect = route.query && route.query.redirect
            },
            immediate: true
        }
    },
    methods: {
        submit(formname) {
            this.$refs[formname].validate((valid) => {
                if (valid) {
                    // 表单验证已通过，执行登录逻辑。
                    // 登录，逻辑在store里实现，因为登录成功之后需要设置store。这里处理的逻辑只是判断是否登录成功，成功就跳转到待跳转页或者首页。
                    this.$store.dispatch('user/userLogin', this.form)
                        .then(() => {
                            this.$message.success('ok')
                        }).catch(error => {
                            this.$message.error(error.message)
                        });
                }
            })
        }
    }
}
</script>

<style>
.login-container {
    height: 100%;
    position: relative;
    display: flex;
    align-items: center;
    justify-content: center;
    
}
.login-box {
    width: 100%;
    max-width: 500px;
    min-width: 300px;
    background-color: rgb(66, 125, 153);
    z-index: 999;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    border-radius: 10px;
}
.login-box .el-form {
    margin: 20px;
    width: 80%;
}
.login-box .el-input {
    margin: 10px 0
}
button {
    width: 100%;
}

#particles-js {
    position: absolute;
    width: 100%;
    height: 100%;
    z-index: 0;
}

</style>